<?php
$blog='NULL';
$blogdata='NULL';
$optiondomain='system';
require('private.php');
require(CONFIGPATH . 'wp-config.php');

$action = $_REQUEST['action'];
$error = '';

nocache_headers();
header( 'Content-Type: text/html; charset='.get_settings('blog_charset') );

if ( defined('RELOCATE') ) { // Move flag is set
	if ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) )
		$_SERVER['PHP_SELF'] = str_replace( $_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF'] );

	$schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
	if ( dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) != get_settings('siteurl') )
		update_option('siteurl', dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) );
}

switch($action) {

case 'logout':

	wp_clearcookie();
//	if (isset($_COOKIE[session_name()]))
/*		setcookie(session_name(), '', time()-42000, '/');
	session_destroy();*/
	do_action('wp_logout');
	nocache_headers();

	$redirect_to = 'login';
	if ( isset($_REQUEST['redirect_to']) )
		$redirect_to = $_REQUEST['redirect_to'];
			
	wp_redirect($redirect_to);
	exit();

break;

case 'login' : 
default:

	$user_login = '';
	$user_pass = '';
	$using_cookie = false;
	if ( !isset( $_REQUEST['redirect_to'] ) || is_user_logged_in() )
		$redirect_to ='profile';//$redirect_to = 'wp-admin/';
	else
		$redirect_to = $_REQUEST['redirect_to'];

	if( $_POST ) {
		$user_login = $_POST['log'];
		$user_login = sanitize_user( $user_login );
		$user_pass  = $_POST['pwd'];
		$rememberme = $_POST['rememberme'];
	} else {
		$cookie_login = wp_get_cookie_login();
		if ( ! empty($cookie_login) ) {
			$using_cookie = true;
			$user_login = $cookie_login['login'];
			$user_pass = $cookie_login['password'];
		}
	}

	do_action('wp_authenticate', array(&$user_login, &$user_pass));

	if ( $user_login && $user_pass ) {
		$user = new WP_User(0, $user_login);
	
		// If the user can't edit posts, send them to their profile.
		if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' ) )
			$redirect_to = LURL . '/profile';
	
		if ( wp_login($user_login, $user_pass, $using_cookie) ) {
			if ( !$using_cookie )
				wp_setcookie($user_login, $user_pass, false, '', '', $rememberme);
			do_action('wp_login', $user_login);
			wp_redirect($redirect_to);
			exit;
		} else {
			if ( $using_cookie )			
				$error = __('Your session has expired.');
		}
	} else if ( $user_login || $user_pass ) {
		$error = __('<strong>Error</strong>: The password field is empty.');
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Lyceum &rsaquo; <?php _e('Login') ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=<?php get_settings('blog_charset'); ?>" />
	<link rel="stylesheet" href="<?php echo WEBROOT; ?>/wp-admin/wp-admin.css" type="text/css" />
	<script type="text/javascript">
	function focusit() {
		document.getElementById('log').focus();
	}
	window.onload = focusit;
	</script>
</head>
<body>

<div id="login">
<h1><a href="http://www.de-sci.org/blogs">Lyceum</a></h1>
<?php
if ( $error )
	echo "<div id='login_error'>$error</div>";
?>

<form name="loginform" id="loginform" action="<?php echo WEBROOT ?>/login" method="post">
<p><label><?php _e('Username:') ?><br /><input type="text" name="log" id="log" value="<?php echo attribute_escape(stripslashes($user_login)); ?>" size="20" tabindex="1" /></label></p>
<p><label><?php _e('Password:') ?><br /> <input type="password" name="pwd" id="pwd" value="" size="20" tabindex="2" /></label></p>
<p>
  <label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="3" /> 
  <?php _e('Remember me'); ?></label></p>
<p class="submit">
	<input type="submit" name="submit" id="submit" value="<?php _e('Login'); ?> &raquo;" tabindex="4" />
	<input type="hidden" name="redirect_to" value="<?php echo attribute_escape($redirect_to); ?>" />
</p>
</form>
<ul>
	<li><a href="<?php echo LURL; ?>/" title="<?php _e('Are you lost?') ?>">&laquo; <?php _e('Back to front page') ?></a></li>
<?php if (get_settings('users_can_register')) : ?>
	<li><a href="<?php echo LURL; ?>/register"><?php _e('Register') ?></a></li>
<?php endif; ?>
	<li><a href="<?php echo LURL; ?>/lost-pass.php?action=lostpassword" title="<?php _e('Password Lost and Found') ?>"><?php _e('Lost your password?') ?></a></li>
</ul>
</div>

</body>
</html>
<?php

break;
} // end action switch
?>
